package com.fjd.service.impl;

import com.fjd.context.BaseContext;
import com.fjd.entity.Discount;
import com.fjd.entity.MerchandiseInfo;
import com.fjd.entity.SearchHistory;
import com.fjd.entity.ShoppingProduct;
import com.fjd.mapper.*;
import com.fjd.service.ShoppingService;
import com.fjd.vo.Category12VO;
import com.fjd.vo.CategoryVO;
import com.fjd.vo.MerchandiseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {

    @Autowired
    private MerchandiseInfoMapper merchandiseInfoMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private ShoppingProductMapper shoppingProductMapper;
    @Autowired
    private SearchHistoryMapper searchHistoryMapper;
    @Autowired
    private DiscountMapper discountMapper;
    @Autowired
    private DiscountDetailMapper discountDetailMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 根据分类获取商品列表
     * @return
     */

    public List<MerchandiseVO> listByCatergory(String category) {
        List<MerchandiseInfo> merchandiseInfoList = merchandiseInfoMapper.getByCategory(category);
        List<MerchandiseVO> merchandiseVOList = convertMerchandiseInfoListToMerchandiseVOList(merchandiseInfoList);


        return merchandiseVOList;
    }

    @Override
    public void addHistoryRecord(String record) {
        SearchHistory searchHistory = SearchHistory.builder()
                .SearchRecord(record)
                .customerId(String.valueOf(BaseContext.getCurrentId()))
                .createTime(LocalDateTime.now())
                .build();
        SearchHistory result = searchHistoryMapper.getByCustomerIdAndSearchRecord(searchHistory.getCustomerId(), searchHistory.getSearchRecord());
        if(result == null){
            searchHistoryMapper.insert(searchHistory);
        }else {
            searchHistoryMapper.updateCreateTime(searchHistory);
        }

    }

    @Override
    public List<String> listHistoryRecord() {
        List<String> records =searchHistoryMapper.getByCustomerId(BaseContext.getCurrentId().toString());

        return records;
    }

    /**
     * 根据搜索词搜索相关商品
     * 并且将搜索次加入到搜索记录中
     * @param record
     * @return
     */
    @Override
    public List<MerchandiseVO> search(String record) {

        //根据搜索词查找相关商品
        List<MerchandiseInfo> merchandiseInfoList = merchandiseInfoMapper.getByName(record);
        List<MerchandiseVO> merchandiseVOList = convertMerchandiseInfoListToMerchandiseVOList(merchandiseInfoList);
        return merchandiseVOList;

    }

    @Override
    public void delHistoryRecord() {
        searchHistoryMapper.deleteByCustomerId(BaseContext.getCurrentId());
    }

    public List<MerchandiseVO> convertMerchandiseInfoListToMerchandiseVOList(List<MerchandiseInfo> merchandiseInfoList){
        List<MerchandiseVO> merchandiseVOList = new ArrayList<>();
        for (MerchandiseInfo mdI:merchandiseInfoList){
            MerchandiseVO merchandiseVO = new MerchandiseVO();
            BeanUtils.copyProperties(mdI,merchandiseVO);
            merchandiseVOList.add(merchandiseVO);
        }

        for(MerchandiseVO md : merchandiseVOList){

            //确定商品对应的图片列表
            List<String> images = productImageMapper.getByProductId(md.getMerchandiseId());
            md.setImageList(images);

            //针对每一个商品 在商品列表页展示的价格应该为该商品下某一个规格的最低价格
            //例子可以参考MerchandiseVO中对price字段的注解
            md.setPrice(shoppingProductMapper.getCheapestPrice(md.getMerchandiseId()));


            Discount discount = discountMapper.getByMerchandiseIdAndProcessing(md.getMerchandiseId());
            //折扣不为空 针对该商品有正在进行中的折扣
            //在discount_detail表中查询该商品下的所有规格中最便宜的价格
            if(discount != null){
                md.setDiscountPrice(discountDetailMapper.getCheapestPrice(discount.getId()));
            }
        }
        return merchandiseVOList;
    }


    //TODO: 根据商品id获取商品溯源码
    public List<String> getSource(String record) {
        //获得商品溯源码分段
        List<String> result = new ArrayList<>();
        if (record != null && record.length() == 9) {
            // 获取前三位
            String firstThree = record.substring(0, 3);
            // 获取前六位
            String firstSix = record.substring(0, 6);
            result.add(firstThree);
            result.add(firstSix);
        } else {
            throw new IllegalArgumentException("Invalid product ID. It must be a 9-character string.");
        }
        //将商品本身id也存进列表中
        result.add(record);
        return result;
    }

    @Override
    public List<CategoryVO> getCategory() {
        List<CategoryVO> categoryVOList = productCategoryMapper.getInfo();
        return categoryVOList;
    }

    @Override
    public List<Category12VO> getCategory12() {
        List<Category12VO> categoryVOList = productCategoryMapper.getInfo12();
        return categoryVOList;
    }

    public List<MerchandiseVO> listByCatergory2(String category) {
        List<ShoppingProduct> list = shoppingProductMapper.getDetailByParentId(category);
        List<String> ids = new ArrayList<>();
        for (ShoppingProduct shoppingProduct : list) {
            ids.add(shoppingProduct.getProductId());
        }
        List<MerchandiseInfo> merchandiseInfoList = merchandiseInfoMapper.getByIds(ids);
        List<MerchandiseVO> merchandiseVOList = convertMerchandiseInfoListToMerchandiseVOList(merchandiseInfoList);


        return merchandiseVOList;
    }
}
